﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;

namespace Site
{
    public partial class Index : UserControl
    {
        #region 动画定义
        //菜单
        private Storyboard storyHomeOut;
        private Storyboard storyHomeIn;
        private Storyboard storyNewsOut;
        private Storyboard storyNewsIn;
        private Storyboard storyTechIn;
        private Storyboard storyTechOut;
        private Storyboard storyProductIn;
        private Storyboard storyProductOut;
        private Storyboard storyResolutionIn;
        private Storyboard storyResolutionOut;
        private Storyboard storySuplierIn;
        private Storyboard storySuplierOut;
        private Storyboard storyContactIn;
        private Storyboard storyContactOut;
        #endregion

        Popup p;

        Dictionary<string, Popup> stack = new Dictionary<string, Popup>();

        public Index()
        {
            InitializeComponent();

            //菜单图标渐变效果
            LinearGradientBrush brush = GetOpacityMarkBrush();
            imgHome.OpacityMask = brush;
            imgNews.OpacityMask = brush;
            imgTech.OpacityMask = brush;
            imgProduct.OpacityMask = brush;
            imgResolution.OpacityMask = brush;
            imgSuplier.OpacityMask = brush;
            imgContact.OpacityMask = brush;

            //菜单外发光效果
            DropShadowEffect effect = EffectShadow();
            this.tbHome.Effect = effect;
            this.tbNews.Effect = effect;
            this.tbTech.Effect = effect;
            this.tbProduct.Effect = effect;
            this.tbResolution.Effect = effect;
            this.tbSuplier.Effect = effect;
            this.tbContact.Effect = effect;
        }

        #region
        /// <summary>
        /// 生成遮罩图层
        /// </summary>
        /// <returns></returns>
        private LinearGradientBrush GetOpacityMarkBrush()
        {
            LinearGradientBrush brush = new LinearGradientBrush();
            brush.EndPoint = new Point(0.5, 1);
            brush.StartPoint = new Point(0.5, 0);
            GradientStop stop1 = new GradientStop();
            stop1.Color = Colors.Transparent;
            stop1.Offset = -1;
            brush.GradientStops.Add(stop1);

            GradientStop stop2 = new GradientStop();
            stop2.Color = Colors.Gray;
            stop2.Offset = 1;
            brush.GradientStops.Add(stop2);
            return brush;
        }

        /// <summary>
        /// 构造时间线
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        private DoubleAnimation GetAnimation(int from, int to, DependencyObject target)
        {
            DoubleAnimation animation = new DoubleAnimation();
            animation.From = from;
            animation.To = to;
            animation.Duration = new Duration(new TimeSpan(0, 0, 1));
            Storyboard.SetTarget(animation, target);
            Storyboard.SetTargetProperty(animation, new PropertyPath(Canvas.TopProperty));
            return animation;
        }

        /// <summary>
        /// 菜单滑出效果
        /// </summary>
        /// <param name="outboard"></param>
        /// <param name="inboard"></param>
        /// <param name="obj"></param>
        private void SlideOut(ref Storyboard outboard, ref Storyboard inboard, DependencyObject obj)
        {
            if (null != inboard && inboard.GetCurrentState() != ClockState.Stopped)
            {
                inboard.Pause();
            }
            inboard = null;

            outboard = new Storyboard();
            outboard.Children.Add(GetAnimation(Convert.ToInt32(obj.GetValue(Canvas.TopProperty)), 555, obj));

            if (outboard.GetCurrentState() != ClockState.Stopped)
            {
                outboard.Stop();
            }
            outboard.Begin();
        }

        /// <summary>
        /// 菜单滑入效果
        /// </summary>
        /// <param name="outboard"></param>
        /// <param name="inboard"></param>
        /// <param name="obj"></param>
        private void SlideIn(ref Storyboard outboard, ref Storyboard inboard, DependencyObject obj, EventHandler target)
        {
            if (outboard.GetCurrentState() != ClockState.Stopped)
            {
                outboard.Pause();
            }
            outboard = null;

            (obj as TextBlock).Visibility = Visibility.Collapsed;

            inboard = new Storyboard();
            inboard.Children.Add(GetAnimation(Convert.ToInt32(obj.GetValue(Canvas.TopProperty)), 567, obj));
            inboard.Completed += new EventHandler(target);

            if (inboard.GetCurrentState() != ClockState.Stopped)
            {
                inboard.Stop();
            }
            inboard.Begin();
        }

        /// <summary>
        /// 外发光效果
        /// </summary>
        private DropShadowEffect EffectShadow()
        {
            System.Windows.Media.Effects.DropShadowEffect ds = new System.Windows.Media.Effects.DropShadowEffect();
            ds.Color = Colors.White;
            ds.BlurRadius = 100;
            ds.ShadowDepth = 0;
            return ds;
        }

        /// <summary>
        /// 灯光效果
        /// </summary>
        /// <param name="img"></param>
        private void Light(Image img,int title)
        {
            if (img.Opacity == 1)
            {
                img.Opacity = 0;
                Popup p2=null;
                if (p.Name != img.Name)
                {
                    p2 = p;
                    p2.IsOpen = false;
                    p = stack[img.Name];
                    (p.Child as IndexWindow).Close();
                    stack.Remove(img.Name);
                }
                else
                {
                    (p.Child as IndexWindow).Close();
                    stack.Remove(img.Name);
                    if (stack.Keys.Count > 0)
                    {
                        p2 = stack[stack.Keys.First()];                        
                    }
                }
                p = p2;
                if (null != p)
                {
                    if (p.IsOpen)
                    {
                        p.IsOpen = false;
                    }
                    p.IsOpen = true;
                }
            }
            else
            {
                img.Opacity = 1;
                if (null != p)
                {
                    (p.Child as IndexWindow).Close();
                }
                PopupWin(img, title); //显示弹出窗口
            }
        }

        private void PopupWin(Image img,int title)
        {
            IndexWindow indexWindow = new IndexWindow();
            indexWindow.Index = title.ToString();
            Popup p1 = new Popup();
            p1.Child = indexWindow;
            p1.HorizontalOffset = 300;
            p1.VerticalOffset = 50;
            p1.Name = img.Name;
            p1.IsOpen = true;
            p = p1;
            stack.Add(img.Name, p1);
        }
        #endregion

        #region 菜单
        #region Home
        void storyHomeIn_Completed(object sender, EventArgs e)
        {
            this.tbHome.Visibility = Visibility.Collapsed;
        }

        private void ImgHome_MouseEnter(object sender, MouseEventArgs e)
        {
            this.imgHome.OpacityMask = null;
            tbHome.Visibility = Visibility.Visible;

            SlideOut(ref storyHomeOut, ref  storyHomeIn, tbHome);
        }

        private void imgHome_MouseLeave(object sender, MouseEventArgs e)
        {
            this.imgHome.OpacityMask = GetOpacityMarkBrush();

            SlideIn(ref storyHomeOut, ref  storyHomeIn, tbHome, storyHomeIn_Completed);
        }

        private void imgHome_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (null != p)
            {
                p.IsOpen = false;
                p = null;
            }
            App app = (App)Application.Current;
            app.RedirectTo(new Home());
        }
        #endregion

        #region News
        void storyNewsIn_Completed(object sender, EventArgs e)
        {
            this.tbNews.Visibility = Visibility.Collapsed;
        }

        private void imgNews_MouseEnter(object sender, MouseEventArgs e)
        {
            this.imgNews.OpacityMask = null;
            this.tbNews.Visibility = Visibility.Visible;
            SlideOut(ref storyNewsOut, ref storyNewsIn, tbNews);
        }

        private void imgNews_MouseLeave(object sender, MouseEventArgs e)
        {
            this.imgNews.OpacityMask = GetOpacityMarkBrush();
            SlideIn(ref storyNewsOut, ref storyNewsIn, tbNews, storyNewsIn_Completed);
        }

        private void imgNews_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (null != p)
            {
                p.IsOpen = false;
                p = null;
            }
            App app = (App)Application.Current;
            app.RedirectTo(new News());
        }
        #endregion

        #region Technology
        void storyTechIn_Completed(object sender, EventArgs e)
        {
            this.tbTech.Visibility = Visibility.Collapsed;
        }

        private void imgTech_MouseEnter(object sender, MouseEventArgs e)
        {
            this.imgTech.OpacityMask = null;
            this.tbTech.Visibility = Visibility.Visible;
            SlideOut(ref storyTechOut, ref storyTechIn, tbTech);
        }

        private void imgTech_MouseLeave(object sender, MouseEventArgs e)
        {
            this.imgTech.OpacityMask = GetOpacityMarkBrush();
            SlideIn(ref storyTechOut, ref storyTechIn, tbTech, storyTechIn_Completed);
        }

        private void imgTech_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (null != p)
            {
                p.IsOpen = false;
                p = null;
            }
            App app = (App)Application.Current;
            app.RedirectTo(new Technology());
        }
        #endregion

        #region Product
        void storyProductIn_Completed(object sender, EventArgs e)
        {
            this.tbProduct.Visibility = Visibility.Collapsed;
        }

        private void imgProduct_MouseEnter(object sender, MouseEventArgs e)
        {
            this.tbProduct.OpacityMask = null;
            this.tbProduct.Visibility = Visibility.Visible;
            SlideOut(ref storyProductOut, ref storyProductIn, tbProduct);
        }

        private void imgProduct_MouseLeave(object sender, MouseEventArgs e)
        {
            this.imgProduct.OpacityMask = GetOpacityMarkBrush();
            SlideIn(ref storyProductOut, ref storyProductIn, tbProduct, storyProductIn_Completed);
        }

        private void imgProduct_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (null != p)
            {
                p.IsOpen = false;
                p = null;
            }
            App app = (App)Application.Current;
            app.RedirectTo(new Product());
        }
        #endregion

        #region Resolution
        void storyResolutionIn_Completed(object sender, EventArgs e)
        {
            this.tbResolution.Visibility = Visibility.Collapsed;
        }

        private void imgResolution_MouseEnter(object sender, MouseEventArgs e)
        {
            this.imgResolution.OpacityMask = null;
            this.tbResolution.Visibility = Visibility.Visible;
            SlideOut(ref storyResolutionOut, ref storyResolutionIn, tbResolution);
        }

        private void imgResolution_MouseLeave(object sender, MouseEventArgs e)
        {
            this.imgResolution.OpacityMask = GetOpacityMarkBrush();
            SlideIn(ref storyResolutionOut, ref storyResolutionIn, tbResolution, storyResolutionIn_Completed);
        }

        private void imgResolution_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (null != p)
            {
                p.IsOpen = false;
                p = null;
            }
            App app = (App)Application.Current;
            app.RedirectTo(new Solution());
        }
        #endregion

        #region Supplier
        void storySuplier_Completed(object sender, EventArgs e)
        {
            this.tbSuplier.Visibility = Visibility.Collapsed;
        }

        private void imgSuplier_MouseEnter(object sender, MouseEventArgs e)
        {
            this.imgSuplier.OpacityMask = null;
            this.tbSuplier.Visibility = Visibility.Visible;
            SlideOut(ref storySuplierOut, ref storySuplierIn, tbSuplier);
        }

        private void imgSuplier_MouseLeave(object sender, MouseEventArgs e)
        {
            this.imgSuplier.OpacityMask = GetOpacityMarkBrush();
            SlideIn(ref storySuplierOut, ref storySuplierIn, tbSuplier, storySuplier_Completed);
        }

        private void imgSuplier_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (null != p)
            {
                p.IsOpen = false;
                p = null;
            }
            App app = (App)Application.Current;
            app.RedirectTo(new Supplier());
        }
        #endregion

        #region Contact
        void storyContact_Completed(object sender, EventArgs e)
        {
            this.tbContact.Visibility = Visibility.Collapsed;
        }

        private void imgContact_MouseEnter(object sender, MouseEventArgs e)
        {
            this.imgContact.OpacityMask = null;
            this.tbContact.Visibility = Visibility.Visible;
            SlideOut(ref storyContactOut, ref storyContactIn, tbContact);
        }

        private void imgContact_MouseLeave(object sender, MouseEventArgs e)
        {
            this.imgContact.OpacityMask = GetOpacityMarkBrush();
            SlideIn(ref storyContactOut, ref storyContactIn, tbContact, storyContact_Completed);
        }

        private void imgContact_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (null != p)
            {
                p.IsOpen = false;
                p = null;
            }
            App app = (App)Application.Current;
            app.RedirectTo(new Contact());
        }
        #endregion
        #endregion

        #region 亮灯效果
        private void imgLight1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Light(imgLight1,1);
        }

        private void imgLight2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Light(imgLight2,2);
        }

        private void imgLight3_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Light(imgLight3,3);
        }

        private void imgLight4_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Light(imgLight4,4);
        }

        private void imgLight5_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Light(imgLight5,5);
        }

        private void imgLight6_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Light(imgLight6,6);
        }

        private void imgLight7_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Light(imgLight7,7);
        }

        private void imgLight8_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Light(imgLight8,8);
        }

        private void imgLight9_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Light(imgLight9,9);
        }

        private void imgLight10_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Light(imgLight10,10);
        }
        #endregion
    }
}
